#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
    // 创建命名管道
    if (mkfifo("mypipe", 0666) == -1)
    {
        perror("mkfifo");
        return 1;
    }
    pid_t id = fork();
    if (id == 0)
    {
        // 子进程
        int fd = open("mypipe", O_RDONLY);
        if (fd == -1)
        {
            std::cerr << "Read Open failed." << std::endl;
            return 1;
        }
        char buffer[1024];
        read(fd, buffer, 1024);
        std::cout << "buffer : " << buffer << std::endl;
        close(fd);
        exit(-1);
    }
    // 父进程
    int fd = open("mypipe", O_WRONLY);
    if (fd == -1)
    {
        std::cerr << "Write Open failed." << std::endl;
        return 1;
    }
    // 向管道中写入数据
    const char *message = "i am process a";
    write(fd, message, strlen(message) + 1);
    close(fd);
    wait(NULL);

    return 0;
}